package com.xhl.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.xhl.service.PropertyService;
import com.xhl.util.SpringContextUtil;
import com.xhl.vo.Pager;
import com.xhl.vo.PropertyVO;

@Controller
@RequestMapping("/property")
public class PropertyController {


    @Autowired
    PropertyService propertyService;


    @GetMapping(value = "/query", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String queryProperty(HttpServletRequest req, HttpServletResponse resp) {
        String cid = req.getParameter("cid");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        if (cid == null) {
            return "异常";
        }

        @SuppressWarnings("unchecked")
        Pager<PropertyVO> p = SpringContextUtil.getBean(Pager.class);

        p.setSize(Integer.valueOf(pageSize));
        p.setPage((Integer.valueOf(pageNum) - 1) * Integer.valueOf(pageSize));
        p.setId(Integer.valueOf(cid));

        List<PropertyVO> plist = propertyService.queryProperty(p);
        Long count = propertyService.count(cid);
        p.setRows(plist);
        p.setTotal(count);
        String json = JSON.toJSONString(p);
        return json;
    }

    @GetMapping(value = "/delete", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteProperty(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        if (id == null) {
            return "异常";
        }
        propertyService.deleteProperty(id);
        return "删除成功";
    }

    @PostMapping(value = "/add", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addProperty(HttpServletRequest req, HttpServletResponse resp) {
        String cid = req.getParameter("cid");
        String name = req.getParameter("name");
        System.out.println("添加cid=" + cid);

        if (cid == null) {
            return "";
        }
        PropertyVO pvo = SpringContextUtil.getBean(PropertyVO.class);
        pvo.setCid(Integer.valueOf(cid));
        pvo.setName(name);
        propertyService.addProperty(pvo);
        return "添加成功";
    }

    @PostMapping(value = "/update", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateProperty(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        System.out.println("修改  id: " + id + "  name : " + name);
        if (id == null) {
            return "";
        }
        PropertyVO pvo = SpringContextUtil.getBean(PropertyVO.class);
        pvo.setId(Integer.valueOf(id));
        pvo.setName(name);
        propertyService.updateProperty(pvo);
        return "修改成功";
    }
}
